// $Id: Cabinet.java 56 2011-11-11 22:41:06Z huijzer@gmail.com $
package lime.model;

import java.util.ArrayList;
import java.util.List;

import lime.Activator;
import lime.service.RepositoryService;
import lime.service.SpringApplicationContext;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.model.IWorkbenchAdapter;
import org.eclipse.ui.plugin.AbstractUIPlugin;

public class Cabinet extends Folder implements IWorkbenchAdapter {

	private boolean isPrivate;
	
	private RepositoryService repositoryService;
	
	public Cabinet(String id, String name) {
		super(id, name);
		repositoryService = (RepositoryService) SpringApplicationContext.getBean("repositoryService"); 
	}
	
	public boolean isPrivate() {
		return isPrivate;
	}

	public void setPrivate(boolean isPrivate) {
		this.isPrivate = isPrivate;
	}


	// ========================================
	// IWorkbenchAdapter
	// ========================================	
	
	@Override
	public Object[] getChildren(Object o) {
		List<RepositoryObject> repositoryObjectList = new ArrayList<RepositoryObject>(); 
		repositoryObjectList.addAll(repositoryService.getFolders(this.getObjectId()));
		repositoryObjectList.addAll(repositoryService.getDocuments(this.getObjectId()));
		return repositoryObjectList.toArray();
	}

	@Override
	public ImageDescriptor getImageDescriptor(Object object) {
		String imageFilePath = isPrivate ? "icons/blue-folder-stand.png" : "icons/folder-stand.png";
		return AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, imageFilePath);
	}

	@Override
	public String getLabel(Object o) {
		return this.getObjectName();
	}

	@Override
	public Object getParent(Object o) {
		return null;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Cabinet) {
			Cabinet doc = (Cabinet) obj;
			return this.getObjectId().equals(doc.getObjectId());
		}
		return false;
	}
	
}
